﻿using AutoMapper;
using BW.MesProject.Production.Domain.Dto.Input;
using BW.MesProject.Production.Domain.Dto.Input.Packing;
using BW.MesProject.Production.Domain;
using BW.MesProject.Production.Infrastructure;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.Public.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BW.MesProject.Production.Domain.Model;

namespace BW.MesProject.Production.Application
{
    public class PackingServices : IPackingServices
    {
        private readonly IBaseRepository<PACKINGMODEL> _packingRepository;
        private readonly IMapper _mapper;
        public PackingServices(IBaseRepository<PACKINGMODEL> packingRepository, IMapper mapper)
        {
            _packingRepository = packingRepository;
            _mapper = mapper;
        }

        public ApiResult<int> CreatePacking(AddPackingInput input)
        {
            PACKINGMODEL model = _mapper.Map<PACKINGMODEL>(input);
            model.CREATETIME = DateTime.Now;
            model.ISDELETED = false;
            model.CREATEUSERNAME = "admin";
            int r = _packingRepository.Add(model);
            if (r > 0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "包装创建成功",
                    Data = r
                };
            }
            return new ApiResult<int>()
            {
                Code = ApiEnums.Error,
                Message = "包装创建失败",
                Data = r
            };
        }

        public ApiResult<int> DeletePacking(long id)
        {
            PACKINGMODEL model = _packingRepository.GetValue(x => x.PACKINGID == id);
            if (model == null)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Fail,
                    Message = "包装不存在",
                    Data = 0
                };
            }
            int r = _packingRepository.Delete(model);
            if (r > 0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "包装删除成功",
                    Data = r
                };
            }
            return new ApiResult<int>()
            {
                Code = ApiEnums.Error,
                Message = "包装删除失败",
                Data = r
            };
        }

        public ApiResult<PackingInput> GetPackingById(long id)
        {
            PACKINGMODEL model = _packingRepository.GetValue(x => x.PACKINGID == id);
            PackingInput input = _mapper.Map<PackingInput>(model);
            if (input == null)
            {
                return new ApiResult<PackingInput>()
                {
                    Code = ApiEnums.Fail,
                    Message = "包装不存在",
                    Data = null
                };
            }
            return new ApiResult<PackingInput>()
            {
                Code = ApiEnums.Success,
                Message = "包装获取成功",
                Data = input
            };
        }

        public ApiResult<List<PACKINGMODEL>> GetPackingList()
        {
            var query = _packingRepository.GetAll()
                .Where(x => (bool)!x.ISDELETED)
                .OrderByDescending(x => x.CREATETIME);
            int total = query.Count();
            if (query==null)
            {
                return new ApiResult<List<PACKINGMODEL>>()
                {
                    Code = ApiEnums.Fail,
                    Message = "没有找到包装数据",
                    Data = null,
                };
            }
            else
            {
                return new ApiResult<List<PACKINGMODEL>>()
                {
                    Code = ApiEnums.Success,
                    Message = "包装列表获取成功",
                    Data = query.ToList(),
                    TotalCount = total
                };
            }

        }

        public ApiResult<int> UpdatePacking(PackingInput input)
        {
            PACKINGMODEL model = _packingRepository.GetValue(x => x.PACKINGID == input.PACKINGID);
            model = _mapper.Map(input,model);
            model.UPDATETIME = DateTime.Now;
            model.UPDATEUSERNAME = "admin";
            int r = _packingRepository.Update(model);
            if (r > 0)
            {
                return new ApiResult<int>()
                {
                    Code = ApiEnums.Success,
                    Message = "包装更新成功",
                    Data = r
                };
            }
            return new ApiResult<int>()
            {
                Code = ApiEnums.Error,
                Message = "包装更新失败",
                Data = r
            };

        }
    }
}
